#include <stdio.h>
#pragma GCC diagnostic ignored "-Wformat="

#include <stdint.h>   // for uintptr_t

#define CYCLE_SORT_IMPLEMENTATION
#include "cycle_sort.h"

#include "acutest.h"

#ifndef UNUSED
#define UNUSED(X) ((void)(X))
#endif

#ifndef E
#define E(X) (sizeof(X)/sizeof(X[0]))
#endif

static inline bool is_sorted_i(const csort_t *data, size_t elements)
{
    for (size_t i = 0; i < elements - 1; i++)
    {
        if (data[i] > data[i + 1])
        {
            return false;
        }
    }
    return true;
}

static void test_integer_sort(void)
{
    static const csort_t test_data[] = { 1, 2, 1, 3, 4, 2, 6, 7, 9, 8, 5, 5, 8 };
    static const csort_t sorted_data[] = { 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9 };
    static csort_t working_data[E(test_data)];

    memcpy(working_data, test_data, sizeof(test_data));
    cycle_sort(working_data, E(working_data));
    TEST_CHECK(is_sorted_i(working_data, E(working_data)));
    TEST_CHECK(!memcmp(sorted_data, working_data, sizeof(sorted_data)));
    TEST_DUMP("test_data:   ", test_data, E(test_data) * sizeof(csort_t));
    TEST_DUMP("working_data:", working_data, E(working_data) * sizeof(csort_t));
    TEST_DUMP("sorted_data: ", sorted_data, E(sorted_data) * sizeof(csort_t));
}

TEST_LIST =
{
    { "integer sort", test_integer_sort },

    { NULL, NULL }
};
